<?php

namespace app\admin\model\user;
use app\admin\model\wxapp\Setting;
use app\common\model\User as UserModel;


/**
 * 用户模型
 * Class User
 * @package app\store\model
 */
class User extends UserModel
{
    /**
     * 关联小程序商城表
     * @return \think\model\relation\BelongsTo
     */
    public function wxapp()
    {
        return $this->belongsTo("app\admin\model\wxapp\Wxapp");
    }





    /**
     * 从缓存中获取商城名称
     * @param $data
     * @return array
     */
    public function getStoreName($data)
    {
        $names = [];
        foreach ($data as $wxapp) {
            if($wxapp['wxapp_id']==0){
                $names[$wxapp['wxapp_id']] = "后台用户";
            }else{
                $names[$wxapp['wxapp_id']] = Setting::getItem('store', $wxapp['wxapp_id'])['name'];

            }

        }

        return $names;
    }


    /**
     * 获取当前用户总数
     * @param null $day
     * @return int|string
     * @throws \think\Exception
     */
    public function getUserTotal($day = null)
    {
        if (!is_null($day)) {
            $startTime = strtotime($day);
            $this->useGlobalScope(false)->where('create_time', '>=', $startTime)
                ->where('create_time', '<', $startTime + 86400);
        }
        return $this->where('is_delete', '=', '0')->count();
    }

    /**
     * 获取用户列表
     * @param string $nickName 昵称
     * @param int $gender 性别
     * @param int $grade 会员等级
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($nickName = '', $gender = -1)
    {

        $where=[];
        // 检索：微信昵称
        if(!empty($nickName))
        {
            $where=['nickName'=>['like', "%$nickName%"]];
        }

        // 检索：性别
        if ($gender !== '' && $gender > -1) {
            $where=['gender'=>(int)$gender];
        }
        // 获取用户列表

        return $this->useGlobalScope(false)
            ->where($where)
            ->where('is_delete', '=', '0')
            ->order(['create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => \request()->request()
            ]);
    }




}
